// packageE/community_buying/index/index.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    infoData: {},
    show: false,
    is_Poster: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let language = wx.getStorageSync("langIndex");
    this.setData({ language: language.en });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},
  gotoGroupBuying() {
    wx.navigateTo({
      url: "/packageE/community_buying/group_buying/group_buying",
    });
  },
  gotoMyInfo() {
    wx.navigateTo({
      url: "/packageE/community_buying/edit_info/edit_info",
    });
  },
  gotoOrder() {
    wx.navigateTo({
      url: "/packageE/community_buying/buying_order/buying_order",
    });
  },
  gotoReward() {
    wx.navigateTo({
      url: "/packageC/selfCarry/selfCarry_reward/selfCarry_reward",
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},
  preview(e) {
    let currentUrl = [];
    wx.previewImage({
      current: this.data.is_Poster, // 当前显示图片的http链接
      urls: currentUrl // 需要预览的图片http链接列表
    });
  },
  saveImg() {
    // var _this = this;
    // var times = _this.data.timeend - _this.data.timestart;
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success() {
              console.log('授权成功');
            }
          });
        }
      }
    });
    var imgSrc = this.data.is_Poster;
    wx.downloadFile({
      url: imgSrc,
      success: function (res) {
        //图片保存到本地
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: function (data) {
            wx.showToast({
              title: '保存成功',
              icon: 'none',
              duration: 2000
            });
          },
          fail: function (err) {
            if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
              console.log("当初用户拒绝，再次发起授权");
              wx.openSetting({
                success(settingdata) {
                  if (settingdata.authSetting['scope.writePhotosAlbum']) {
                    wx.showToast({
                      icon: 'none',
                      title: '获取权限成功,再次长按保存图片',
                      duration: 1500
                    });
                  } else {
                    wx.showToast({
                      icon: 'none',
                      title: '获取权限失败,保存图片需要用户授权',
                      duration: 1500
                    });
                  }
                }
              });
            }
          },
          complete(res) {
            console.log(res);
          }
        });
      }
    });
  },
  onCopy(){
    wx.setClipboardData({
      data: "/packageF/others/deliverPoster/deliverPoster?id="+this.data.infoData.deliver_id,
      success(res) {
        wx.showToast({
          icon: "none",
          title: "复制成功",
          duration: 1000,
        });
      },
    });
  },
  onClose(){
    this.setData({
      show:false
    })
  },
  getposter(){
    let urlStr = app.getNetAddresss(
      "plugin.package-deliver.frontend.deliver-share.getPoster"
    );
    app._postNetWork({
      url: urlStr,
      data:{
        deliver_id:this.data.infoData.deliver_id
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          this.setData({ 
            is_Poster:res.data.url,
            show:true
          });
        }
      },
    });
  },
  getData() {
    let urlStr = app.getNetAddresss(
      "plugin.package-deliver.frontend.deliver.getDeliverBonusAmount"
    );
    app._postNetWork({
      url: urlStr,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          this.setData({ infoData: res.data });
        }
      },
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
